//
//  ZYTianQiViewController.m
//  XiangMuYiBai
//
//  Created by mac on 15-4-20.
//  Copyright (c) 2015年 zhiyou. All rights reserved.
//

#import "ZYTianQiViewController.h"
@interface ZYTianQiViewController ()

@end

@implementation ZYTianQiViewController
@synthesize isBaiTian;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
   UIImageView* bg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
    bg.image=[UIImage imageNamed:@"w_bg.png"];
    [self.view addSubview:bg];
    UIImageView *touTiao=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];
    touTiao.image=[UIImage imageNamed:@"w_bg2@2x.png"];
    [self.view addSubview:touTiao];
    UIButton *back=[UIButton buttonWithType:UIButtonTypeSystem];
    back.frame=CGRectMake(20, 20, 20, 20);
    [back setBackgroundImage:[UIImage imageNamed:@"back@2x.png"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:back];
    UIButton *shiJian=[UIButton buttonWithType:UIButtonTypeSystem];
    shiJian.frame=CGRectMake(250, 10, 60, 40);
    [shiJian setTitle:@"白天" forState:UIControlStateNormal];
    shiJian.titleLabel.font=[UIFont systemFontOfSize:25];
    [shiJian setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    shiJian.tag=10;
    [shiJian addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:shiJian];
    UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 400, 320, 80)];
    scrollView.contentSize=CGSizeMake((90+40)*7, 80);
    scrollView.showsHorizontalScrollIndicator=NO;
    scrollView.pagingEnabled=YES;
    [self.view addSubview:scrollView];
    NSArray *ary=@[@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",@"周日"];
    array=[[NSMutableArray alloc]init];
    for (int i=0 ; i<7; i++)
    {
        UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
        button.frame=CGRectMake(20+i*(90+40), 20, 90, 50);
        [button setBackgroundImage:[UIImage imageNamed:@"w_xq2.png"] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        NSString *str=[ary objectAtIndex:i];
        [button setTitle:str forState:UIControlStateNormal];
        button.tag=i+1;
        [button addTarget:self action:@selector(shiJian:) forControlEvents:UIControlEventTouchUpInside];
        [array addObject:button];
        [scrollView addSubview:button];
    }
    btnXiang=[UIButton buttonWithType:UIButtonTypeCustom];
    btnXiang.frame=CGRectMake(20, 20, 90, 50);
    [scrollView addSubview:btnXiang];
    [btnXiang setBackgroundImage:[UIImage imageNamed:@"w_xq1"] forState:UIControlStateNormal];
    [btnXiang setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnXiang setTitle:@"周一" forState:UIControlStateNormal];
    btnZhi=[UIButton buttonWithType:UIButtonTypeCustom];
    btnZhi.frame=CGRectMake(20, 0, 90, 20);
    [btnZhi setBackgroundImage:[UIImage imageNamed:@"w_tip"] forState:UIControlStateNormal];
    [scrollView addSubview:btnZhi];
    [self tianQi:@"w_qing.png" zhenDongHua:@[[UIImage imageNamed:@"w_sun1"],[UIImage imageNamed:@"w_sun2"]] xingXing:nil yun:nil yu:nil];
    a=1;

}
-(void)tianQi:(NSString*)str zhenDongHua:(NSArray*)array1 xingXing:(NSArray*)array2 yun:(NSString*)str2 yu:(NSString*)str3
{
    UIImageView *imageView1=[[UIImageView alloc]initWithFrame:CGRectMake(15, 80, 290, 320)];
    imageView1.image=[UIImage imageNamed:str];
    [self.view addSubview:imageView1];
    if (array1!=nil)
    {
        UIImageView *taiYang=[[UIImageView alloc]init];
        taiYang.center=CGPointMake(200, 100);
        taiYang.bounds=CGRectMake(0, 0, 150, 150);
        taiYang.animationImages=array1;
        taiYang.animationDuration=.2;
        [taiYang startAnimating];
        [imageView1 addSubview:taiYang];
    }
    if (array2!=nil)
    {
        for (int i=0; i<20; i++)
        {
            UIImageView *taiYang=[[UIImageView alloc]init];
            taiYang.center=CGPointMake(arc4random()%290, arc4random()%320);
            taiYang.bounds=CGRectMake(0, 0, 20, 20);
            taiYang.animationImages=array2;
            taiYang.animationDuration=.2;
            [taiYang startAnimating];
            [imageView1 addSubview:taiYang];
        }
    }


    if (str2 != nil)
    {
        array3=[[NSMutableArray alloc]init];
        for (int i=0; i<4; i++)
        {
            UIImageView *imageView=[[UIImageView alloc]init];
            imageView.image=[UIImage imageNamed:str2];
            imageView.tag=1;
            [imageView1 addSubview:imageView];
            [array3 addObject:imageView];
        }
      NSTimer *time=  [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(time) userInfo:nil repeats:YES];
      NSTimer *time1 = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(fei) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop]addTimer:time forMode:NSRunLoopCommonModes];
        [[NSRunLoop currentRunLoop]addTimer:time1 forMode:NSRunLoopCommonModes];
        [time fire];
    }

    if (str3 != nil)
    {
        array4=[[NSMutableArray alloc]init];
        for (int i=0; i<20; i++)
        {
            UIImageView *imageView=[[UIImageView alloc]init];
            imageView.image=[UIImage imageNamed:str3];
            imageView.tag=1;
            [imageView1 addSubview:imageView];
            [array4 addObject:imageView];
        }
      NSTimer *time3 = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(time2) userInfo:nil repeats:YES];
      NSTimer *time4 = [NSTimer scheduledTimerWithTimeInterval:.008 target:self selector:@selector(fei2) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop]addTimer:time3 forMode:NSRunLoopCommonModes];
        [[NSRunLoop currentRunLoop]addTimer:time4 forMode:NSRunLoopCommonModes];
    }

}
-(void)fei
{
    for (UIImageView*imageView in array3)
    {
        if (imageView.tag==2)
        {
            if (imageView.center.x>-60)
            {
                imageView.center=CGPointMake(imageView.center.x-1, imageView.center.y);
            }
            else
            {
                imageView.tag=1;
            }
        }
    }
    
}
-(void)time2
{
    for (UIImageView*imageView in array4)
    {
        if (imageView.tag==1)
        {
            imageView.tag=2;
            imageView.frame=CGRectMake(arc4random()%275, 0, 15, 15);
            break;
        }
    }
    
}
-(void)fei2
{
    for (UIImageView*imageView in array4)
    {
        if (imageView.tag==2)
        {
            if (imageView.center.y<315)
            {
                imageView.center=CGPointMake(imageView.center.x, imageView.center.y+1);
            }
            else
            {
                imageView.tag=1;
            }
        }
    }

}
-(void)time
{
    for (UIImageView*imageView in array3)
    {
    if (imageView.tag==1)
    {
        imageView.tag=2;
        imageView.frame=CGRectMake(290, arc4random()%200, 110, 60);
        break;
    }
    }
    
}
-(void)click:(UIButton*)btn
{
    if (!isBaiTian)
    {
        isBaiTian=YES;
        [btn setTitle:@"晚上" forState:UIControlStateNormal];
        NSLog(@"晚上");
    }
    else
    {
        isBaiTian=NO;
        [btn setTitle:@"白天" forState:UIControlStateNormal];
        NSLog(@"白天");
    }
    UIButton*button=(UIButton*)[self.view viewWithTag:a];
    [self shiJian:button];
}
-(void)zhi:(int)shu zhouJi:(NSString*)string
{
    btnXiang.frame=CGRectMake(20+shu*(90+40), 20, 90, 50);
    btnZhi.frame=CGRectMake(20+ shu*(90+40), 0, 90, 20);
    [btnXiang setTitle:string forState:UIControlStateNormal];
    [btnXiang setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
-(void)shiJian:(UIButton*)btn
{
    switch (btn.tag)
    {
        case 1:
        {
            a=1;
            [self zhi:a-1 zhouJi:@"周一"];
            if (!isBaiTian )
            {
                [self tianQi:@"w_qing.png" zhenDongHua:@[[UIImage imageNamed:@"w_sun1"],[UIImage imageNamed:@"w_sun2"]] xingXing:nil yun:nil yu:nil];
            }
            else
            {
            [self tianQi:@"w_night.png" zhenDongHua:@[[UIImage imageNamed:@"w_moon1"],[UIImage imageNamed:@"w_moon2"]] xingXing:@[[UIImage imageNamed:@"xx1"],[UIImage imageNamed:@"xx2"]] yun:nil yu:nil];
            }
            

        }
            break;
        case 2:
        {
            a=2;
            [self zhi:a-1 zhouJi:@"周二"];
            if (!isBaiTian)
            {
                [self tianQi:@"w_qing.png" zhenDongHua:@[[UIImage imageNamed:@"w_sun1"],[UIImage imageNamed:@"w_sun2"]] xingXing:nil yun:@"w_cloud" yu:nil];
            }
            else
            {
                [self tianQi:@"w_night.png" zhenDongHua:@[[UIImage imageNamed:@"w_moon1"],[UIImage imageNamed:@"w_moon2"]] xingXing:@[[UIImage imageNamed:@"xx1"],[UIImage imageNamed:@"xx2"]] yun:@"w_cloud" yu:nil];
            }
        }
            break;
        case 3:
        {
            a=3;
             [self zhi:a-1 zhouJi:@"周三"];
            if (!isBaiTian)
            {
                [self tianQi:@"w_yin.png" zhenDongHua:nil xingXing:nil yun:nil yu:nil];
            }
            else
            {
                [self tianQi:@"w_night.png" zhenDongHua:nil xingXing:nil yun:nil yu:nil];
            }
        }
            break;
        case 4:
        {
            a=4;
             [self zhi:a-1 zhouJi:@"周四"];
            if (!isBaiTian)
            {
                [self tianQi:@"w_yu.png" zhenDongHua:nil xingXing:nil yun:nil yu:@"w_rain"];
            }
            else
            {
                [self tianQi:@"w_night.png" zhenDongHua:nil xingXing:nil yun:nil yu:@"w_rain"];
            }
        }
            break;
        case 5:
        {
            a=5;
             [self zhi:a-1 zhouJi:@"周五"];
            if (!isBaiTian)
            {
                [self tianQi:@"w_qing.png" zhenDongHua:@[[UIImage imageNamed:@"w_sun1"],[UIImage imageNamed:@"w_sun2"]] xingXing:nil yun:nil yu:nil];
            }
            else
            {
                [self tianQi:@"w_night.png" zhenDongHua:@[[UIImage imageNamed:@"w_moon1"],[UIImage imageNamed:@"w_moon2"]] xingXing:@[[UIImage imageNamed:@"xx1"],[UIImage imageNamed:@"xx2"]] yun:nil yu:nil];
            }
        }
            break;
        case 6:
        {
            a=6;
             [self zhi:a-1 zhouJi:@"周六"];
            if (!isBaiTian)
            {
                [self tianQi:@"w_qing.png" zhenDongHua:@[[UIImage imageNamed:@"w_sun1"],[UIImage imageNamed:@"w_sun2"]] xingXing:nil yun:nil yu:nil];
            }
            else
            {
                [self tianQi:@"w_night.png" zhenDongHua:@[[UIImage imageNamed:@"w_moon1"],[UIImage imageNamed:@"w_moon2"]] xingXing:@[[UIImage imageNamed:@"xx1"],[UIImage imageNamed:@"xx2"]] yun:nil yu:nil];
            }
        }
            break;
        case 7:
            if (!isBaiTian)
            {
                a=7;
                 [self zhi:a-1 zhouJi:@"周日"];
                [self tianQi:@"w_qing.png" zhenDongHua:@[[UIImage imageNamed:@"w_sun1"],[UIImage imageNamed:@"w_sun2"]] xingXing:nil yun:nil yu:nil];
            }
            else
            {
                [self tianQi:@"w_night.png" zhenDongHua:@[[UIImage imageNamed:@"w_moon1"],[UIImage imageNamed:@"w_moon2"]] xingXing:@[[UIImage imageNamed:@"xx1"],[UIImage imageNamed:@"xx2"]] yun:nil yu:nil];
            }        {
            
        }
            break;
            
        default:
            break;
    }
}
-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
